import { NPagination, PaginationProps } from 'naive-ui';
import OceanComponent from './OceanComponent';

type TPaginationProps = PaginationProps & {
  onUpdated?: Function;
  'on-update:page'?: Function;
  'on-update:page-size'?: Function;
};

type PageData = {
  page: number;
  pageSize: number;
};

interface IPagination {
  setPage(page: number): this;
  setCount(count: number): this;
  getPageData(): Partial<PageData & { pageIndex: number }>;
}

class Pagination<T extends TPaginationProps = TPaginationProps> extends OceanComponent<TPaginationProps> implements IPagination {
  isOceanComponent: boolean = false;
  constructor(attrs?: T) {
    const onUpdated = () => {
      this.attrs?.onUpdated &&
        this.attrs.onUpdated({
          page: this.attrs.page,
          pageSize: this.attrs.pageSize
        });
    };
    super(NPagination, attrs, {
      page: 1,
      pageSize: 10,
      itemCount: 0,
      pageSizes: [10, 20, 30, 40],
      showSizePicker: true,
      'on-update:page': v => {
        this.attrs.page = v;
        onUpdated();
      },
      'on-update:page-size': v => {
        this.attrs.page = 1;
        this.attrs.pageSize = v;
        onUpdated();
      }
    });
  }
  setPage(page: number): this {
    this.attrs.page = page;
    return this;
  }
  setCount(count: number): this {
    this.attrs.itemCount = count;
    return this;
  }
  getPageData(): PageData {
    let { page, pageSize } = this.attrs;
    return {
      page,
      pageSize
    };
  }
}

export default Pagination;
export type { TPaginationProps };
